package leetcode.code0735;

import java.util.Stack;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int[] asteroidCollision(int[] asteroids) {
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < asteroids.length; i++) {
			int a = asteroids[i];
			while (a < 0 && !stack.isEmpty() && stack.peek() > 0 && Math.abs(stack.peek()) < Math.abs(a)) {
				stack.pop();
			}
			if (stack.isEmpty() || stack.peek() * a > 0 || (stack.peek() < 0 && a > 0)) {
				stack.add(a);
			} else if (stack.peek() > 0 && Math.abs(stack.peek()) == Math.abs(a)) {
				stack.pop();
			}
		}
		int[] ans = new int[stack.size()];
		int p = ans.length - 1;
		while (!stack.isEmpty()) {
			ans[p--] = stack.pop();
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
